<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<!--- custom tag for rendering form container --->

<cfif NOT thisTag.hasEndTag>
	<cfthrow type="mod.formutils.form.NoEndTag" message="FormUtils.Form custom tag was called without an end tag." detail="You must use the formutils.form custom tag with an end tag." />
</cfif>
<cfsetting enablecfoutputonly="yes">

<cfparam name="attributes.pageId" default="" />
<cfparam name="attributes.id" default="" />
<cfparam name="attributes.name" default="#attributes.id#" />
<cfparam name="attributes.action" default="" />
<cfparam name="attributes.enctype" default="application/x-www-form-urlencoded" />
<cfparam name="attributes.method" default="post" />
<cfparam name="attributes.title" default="" />
<cfparam name="attributes.style" default="" />
<cfparam name="attributes.class" default="" />
<cfparam name="attributes.onsubmit" default="" />
<cfparam name="attributes.target" default="" />
<cfparam name="attributes.showSubmit" default="true" />
<cfparam name="attributes.submitValue" default=" Submit " />
<cfparam name="attributes.showReset" default="false" />

<cfif isValid('integer', attributes.pageId)>
	<cfset thisPage = request.PS.getPage(attributes.pageId) />
    <cfif len(thisPage.getFormAction()) AND NOT len(attributes.action)>
    	<cfset attributes.action = thisPage.getFormAction() />
    </cfif>
    <cfif len(thisPage.getFormId()) AND NOT len(attributes.id)>
    	<cfset attributes.id = thisPage.getFormId() />
    </cfif>
    <cfif len(thisPage.getFormName()) AND NOT len(attributes.Name)>
    	<cfset attributes.Name = thisPage.getFormName() />
    </cfif>
    <cfif len(thisPage.getFormMethod())>
    	<cfset attributes.method = thisPage.getFormMethod() />
    </cfif>
</cfif>

<cfif arrayLen(request.FS.getFieldsByPage(page=attributes.pageId,fieldtype="file"))>
	<cfset attributes.enctype = "multipart/form-data" />
</cfif>


<cfif thisTag.executionMode EQ 'start'>
	<cfset outStr = '<form' />
	<cfif len(trim(attributes.name))><cfset outStr = outStr & ' name="#trim(attributes.name)#"' /></cfif> 
    <cfif len(trim(attributes.id))><cfset outStr = outStr & ' id="#trim(attributes.id)#"' /></cfif> 
    <cfif len(trim(attributes.action))><cfset outStr = outStr & ' action="#trim(attributes.action)#"' /></cfif> 
    <cfif len(trim(attributes.enctype))><cfset outStr = outStr & ' enctype="#trim(attributes.enctype)#"' /></cfif> 
    <cfif len(trim(attributes.method))><cfset outStr = outStr & ' method="#trim(attributes.method)#"' /></cfif> 
    <cfif len(trim(attributes.title))><cfset outStr = outStr & ' title="#trim(attributes.title)#"' /></cfif> 
    <cfif len(trim(attributes.style))><cfset outStr = outStr & ' style="#trim(attributes.style)#"' /></cfif> 
    <cfif len(trim(attributes.class))><cfset outStr = outStr & ' class="#trim(attributes.class)#"' /></cfif> 
    <cfif len(trim(attributes.onSubmit))><cfset outStr = outStr & ' onSubmit="#trim(attributes.onSubmit)#"' /></cfif> 
    <cfif len(trim(attributes.target))><cfset outStr = outStr & ' target="#trim(attributes.target)#"' /></cfif>
    <cfset outStr = outStr & '>' />
    <cfset outStr = reReplaceNoCase(outStr, '/s', ' ', 'all') />
	<cfoutput>#outStr#</cfoutput>
<cfelse><!--- handle end tag functionality --->
	<cfoutput>
	<cfif attributes.showSubmit>
    	<div class="ctrlHolder">
        	<input name="submit" value="#attributes.submitValue#" type="submit" />
        </div>
    </cfif>
    <cfif attributes.showReset>
    	<div class="ctrlHolder">
        	<input name="reset" value="reset" type="reset" />
        </div>
    </cfif>
	</form></cfoutput>
</cfif>
<cfsetting enablecfoutputonly="no">


